package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.AmortiseSchema;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MembershipService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class AdminAmortiseSchBean extends AbstractBean{

	public static final String BEAN_NAME="adminAmortiseSchBean";
	public static final String NAV_LIST="adminAmortiseSchList";
	public static final String NAV_CREATE="adminAmortiseSchCreate";
	public static final String NAV_EDIT="adminAmortiseSchEdit";
	
	private MembershipService service;
	private AmortiseSchema schema;
	private List<AmortiseSchema> schemaList;
	private List<SelectItem> baseList;
	public AmortiseSchema getSchema() {
		return schema;
	}
	public void setSchema(AmortiseSchema schema) {
		this.schema = schema;
	}

	public void setService(MembershipService service) {
		this.service = service;
	}
	/*------------------------------------------------------------*/
	public String navListAction(){
		return NAV_LIST;
	}
	
	public List<AmortiseSchema> getSchemaList() {
		try{
			schemaList = service.getAmortiseSchemaList();
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return schemaList;
	}
	
	public String navCreateAction(){
		schema = new AmortiseSchema();
		return NAV_CREATE;
	}
	
	public String createAction(){
		try{
			service.createAmortiseSchema(schema, getThisUser());
			return NAV_LIST;
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_CREATE;
	}
	
	public void editActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("schemaId");
		String id = (String)param.getValue();
		for(AmortiseSchema a:schemaList){
			if(a.getAmortiseSchId().equalsIgnoreCase(id)){
				schema = a;
				break;
			}
		}
	}
	
	public String navEditAction(){
		return NAV_EDIT;
	}
	
	public String editAction(){
		try{
			service.editAmortiseSchema(schema, getThisUser());
			return NAV_LIST;
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_EDIT;
	}
	
	public String deleteAction(){
		try{
			service.deleteAmortiseSchema(schemaList, getThisUser());
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_LIST;
	}
	public List<SelectItem> getBaseList() {
		
		baseList = new ArrayList<SelectItem>();
		SelectItem item1 = new SelectItem(CommonConstant.SELECT_MONTHLY,SpringUtils
				.getMessage(CommonConstant.SELECT_MONTHLY, new Object[]{}));
		SelectItem item2 = new SelectItem(CommonConstant.SELECT_YEARLY,SpringUtils
				.getMessage(CommonConstant.SELECT_YEARLY, new Object[]{}));
		baseList.add(item1);
		baseList.add(item2);
		return baseList;
	}
	
}
